﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.ComponentModel.Composition.UnitTesting
{
    public static class CompositionAssert
    {
        public static void ThrowsIssue(string issueId, Action action)
        {
            ThrowsIssue(issueId, action, 1);
        }

        public static void ThrowsIssue(string issueId, Action action, int retryCount)
        {
            ThrowsIssue(issueId, (Exception)null, action, retryCount);
        }

        public static void ThrowsIssue(string issueId, Exception expectedException, Action action, int retryCount)
        {
            ThrowsIssues(new string[] { issueId }, expectedException, action, retryCount);
        }

        public static void ThrowsIssues(string issueId1, string issueId2, Action action)
        {
            ThrowsIssues(new string[] { issueId1, issueId2 }, (Exception)null, action, 1);
        }

        private static void ThrowsIssues(string[] issueId, Exception expectedException, Action action, int retryCount)
        {
            for (int i = 0; i < retryCount; i++)
            {
                CompositionException ex = ExceptionAssert.Throws<CompositionException>(action);

                EnumerableAssert.AreEqual(ex.Issues.Select(issue => issue.Id), issueId);

                if (expectedException != null)
                {
                    Assert.AreSame(expectedException, ex.Issues[0].Exception);
                }
            }
        }
    }
}
